package com.boot.boot2.task;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.Future;

/**
 * Created by Wuxiang on 2018/5/3
 *
 * @Async 实现异步任务的调用
 *
 * Future 是对于具体的 Runnable 或者 Callable 任务的执行结果进行取消、查询是否完成、获取结果的接口。
 *
 * 参考：https://www.tuicool.com/articles/zu6Nfmf
 */
@Component
public class Task {

    public static Random random = new Random();

    @Async("taskExecutor")
    public Future<String> run() throws Exception {
        long sleep = random.nextInt(10000);
        System.out.println("开始任务，需耗时：" + sleep + "毫秒");
        Thread.sleep(sleep);
        System.out.println("完成任务");
        return new AsyncResult<>("test");
    }
}
